package com.zm.cc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zm.cc.Utils;
import com.zm.cc.entity.Fav;
import com.zm.cc.entity.Tag;
import com.zm.cc.entity.TagFav;
import com.zm.cc.mapper.FavMapper;
import com.zm.cc.mapper.TagFavMapper;
import com.zm.cc.mapper.TagMapper;
import com.zm.cc.service.BizException;
import com.zm.cc.service.IFavService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zm
 * @since 2024-08-05
 */
@Service
public class FavServiceImpl extends ServiceImpl<FavMapper, Fav> implements IFavService {
    @Autowired
    TagMapper tagMapper;

    @Autowired
    TagFavMapper tagFavMapper;

    @Override
    @Transactional(rollbackFor = BizException.class)
    public void add(Fav fav) throws BizException {
        //字段验证
        Utils.checkEmpty(fav.getName(),"地址名称不能为空!");
        Utils.checkEmpty(fav.getAddr(),"地址不能为空!");
        Utils.check(fav.getName().length()<2,"地址名称不能小于2个字符");
        // TODO 其他字段自行拓展

        //新增地址
        this.save(fav);

        //  门户;新闻;....
        String[] tags = fav.getTags().split("[;\\s；]+");


        for (String tag : tags) {
            //查询分类
            LambdaQueryWrapper<Tag> lqw = new LambdaQueryWrapper<Tag>();
            lqw.eq(Tag::getUid,fav.getUid());
            lqw.eq(Tag::getName,tag);
            Tag dbtag = tagMapper.selectOne(lqw);
            if (dbtag == null) {
                //没有该类
                dbtag = new Tag();
                dbtag.setName(tag);
                dbtag.setUid(fav.getUid());
                tagMapper.insert(dbtag);
            }else {
                //已有该类
            }
            TagFav tagFav = new TagFav();
            tagFav.setTid(dbtag.getId());
            tagFav.setFid(fav.getId());
            tagFavMapper.insert(tagFav);
        }
    }

    @Override
    public void modyfi(Fav fav) throws BizException {
        //字段验证
        Utils.checkEmpty(fav.getName(),"地址名称不能为空!");
        Utils.checkEmpty(fav.getAddr(),"地址不能为空!");
        Utils.check(fav.getName().length()<2,"地址名称不能小于2个字符");
        // TODO 其他字段自行拓展

        //新增地址
        this.updateById(fav);

        //  门户;新闻;....
        String[] tags = fav.getTags().split("[;\\s；]+");

        LambdaQueryWrapper<TagFav> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(TagFav::getFid,fav.getId());
        tagFavMapper.delete(lqw1);
        for (String tag : tags) {
            //查询分类
            LambdaQueryWrapper<Tag> lqw = new LambdaQueryWrapper<Tag>();
            lqw.eq(Tag::getUid,fav.getUid());
            lqw.eq(Tag::getName,tag);
            Tag dbtag = tagMapper.selectOne(lqw);
            if (dbtag == null) {
                //没有该类
                dbtag = new Tag();
                dbtag.setName(tag);
                dbtag.setUid(fav.getUid());
                tagMapper.insert(dbtag);
            }else {
                //已有该类
            }
            TagFav tagFav = new TagFav();
            tagFav.setTid(dbtag.getId());
            tagFav.setFid(fav.getId());
            tagFavMapper.insert(tagFav);
        }
    }

    @Override
    public int deleteFav(Fav fav) {
        removeById(fav.getId());
        LambdaQueryWrapper<TagFav> lqw = new LambdaQueryWrapper<>();
        lqw.eq(TagFav::getFid,fav.getId());
        int result = tagFavMapper.delete(lqw);
        return result;
    }
}
